package com.daikin.ktmustwrite.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.daikin.ktmustwrite.app.network.apiService
import com.daikin.ktmustwrite.app.network.stateCallback.ListDataUIState
import com.daikin.ktmustwrite.data.model.bean.AriticleResponse
import com.daikin.ktmustwrite.data.model.bean.NavigationResponse
import com.daikin.ktmustwrite.data.model.bean.SystemResponse
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

class RequestTreeViewModel : BaseViewModel() {
    //页码，体系 广场的页码是从0开始的
    private var pageNo = 0

    //广场数据
    var plazaDataState: MutableLiveData<ListDataUIState<AriticleResponse>> = MutableLiveData()
    //每日一问数据
    var askDataState: MutableLiveData<ListDataUIState<AriticleResponse>> = MutableLiveData()
    //体系数据
    var systemDataState: MutableLiveData<ListDataUIState<SystemResponse>> = MutableLiveData()
    //导航数据
    var navigationDataState: MutableLiveData<ListDataUIState<NavigationResponse>> =
        MutableLiveData()
    /**
     * @Description:获取广场数据
     */
    fun getPlazaData(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 0
        }
        request({ apiService.getSquareData(pageNo) }, {
            //请求成功
            pageNo++
            val listDataUiState =
                ListDataUIState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    hasMore = it.hasMore(),
                    isFirstEmpty = isRefresh && it.isEmpty(),
                    listData = it.datas
                )
            plazaDataState.value = listDataUiState
        }, {
            //请求失败
            val listDataUiState =
                ListDataUIState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    isRefresh = isRefresh,
                    listData = arrayListOf<AriticleResponse>()
                )
            plazaDataState.value = listDataUiState
        })
    }


    /**
     * 获取每日一问数据
     */
    fun getAskData(isRefresh: Boolean){
        if (isRefresh){
            pageNo=1
        }
        request({ apiService.getAskData(pageNo)},{
            pageNo++
            val  listDataUiState =
                ListDataUIState(isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    hasMore = it.hasMore(),
                    isFirstEmpty = isRefresh && it.isEmpty(),
                    listData = it.datas)
            askDataState.value = listDataUiState
        },{
            //请求失败
            val listDataUiState =
                ListDataUIState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    isRefresh = isRefresh,
                    listData = arrayListOf<AriticleResponse>()
                )
            plazaDataState.value = listDataUiState
        })

    }

    /**
     * 获取体系数据
     */
    fun getSystemData(){
        request({
            apiService.getSystemData()
        },{
            //请求成功
            val dataUiState =
                ListDataUIState(
                    isSuccess = true,
                    listData = it
                )
            systemDataState.value = dataUiState
        }, {
            //请求失败
            val dataUiState =
                ListDataUIState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    listData = arrayListOf<SystemResponse>()
                )
            systemDataState.value = dataUiState
        })
    }


    /**
     * 获取导航数据
     */
    fun getNavigationData() {
        request({ apiService.getNavigationData() }, {
            //请求成功
            val dataUiState =
                ListDataUIState(
                    isSuccess = true,
                    listData = it
                )
            navigationDataState.value = dataUiState
        }, {
            //请求失败
            val dataUiState =
                ListDataUIState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    listData = arrayListOf<NavigationResponse>()
                )
            navigationDataState.value = dataUiState
        })
    }

}